﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Company.Web.Areas.Manage.Models.NewsModels;
using Code.BLL;
using Company.Web.Commen;
using Code.Models;

namespace Company.Web.Areas.Manage.Controllers
{
    public class NewsController : ControllerBase
    {
        NewsManage NM = new NewsManage();
        public ActionResult Index(IndexModel model)
        {
            if (model == null)
                model = new IndexModel();
            if (model.Serch == null)
                model.Serch = new IndexSerchModel();
            if (model.ID < 1)
                model.ID = null;
            if (model.A < 1)
                model.A = null;
            var page = model.ID ?? 1;
            model.Size = model.A ?? 10;
            model.Languages = (Enum.GetValues(typeof(Language)) as Language[]).Select(p => new SelectListItem { Value = p.ToString(), Text = p.Name() }).ToList();
            model.Languages.Insert(0, new SelectListItem { Text = "所有", Value = "" });

            var data = NM.GetTable<New>();
            if (!string.IsNullOrEmpty(model.Serch.Title))
                data = data.Where(p => p.Title.Contains(model.Serch.Title));
            if (model.Serch.StartTime.HasValue)
                data = data.Where(p => p.AddTime >= model.Serch.StartTime.Value.Date);
            if (model.Serch.EndTime.HasValue)
                data = data.Where(p => p.AddTime < model.Serch.EndTime.Value.AddDays(1).Date);
            if (model.Serch.Language.HasValue)
                data = data.Where(p => p.Language == (short)model.Serch.Language);
            model.SumCount = data.Count();

            model.News = data.OrderByDescending(p => p.AddTime)
                .Skip(model.Size * (page - 1)).Take(model.Size)
                .Select(p => new NewItem
                {
                    ID = p.ID,
                    Title = p.Title,
                    Content = p.Content,
                    AddTime = p.AddTime,
                    ClickCount=p.ClickCount,
                    State=(NewState)p.State,
                    Highlight=p.Highlight,
                    Language = (Language)p.Language
                }).ToArray();

            return View(model);
        }

        public ActionResult Edit(long id)
        {
            var data = NM.GetTable<New>().FirstOrDefault(p => p.ID == id);
            if (data == null)
                return Content(Url.Action("index").Redirect("不存在该新闻"));

            var model = new EditNewModel
            {
                ID=id,
                AddTime=data.AddTime,
                Title=data.Title,
                Content=data.Content,
                Highlight=data.Highlight,
                Language=(Language)data.Language,
                Lanaguages = (Enum.GetValues(typeof(Language)) as Language[]),
                Action = "edit",
                State = NewState.Enabled,
            };
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(EditNewModel model)
        {
            if (!model.ID.HasValue)
                return RedirectToAction("index");
            if (ModelState.IsValid)
            { 
                var _new = new New
                {
                    ID=model.ID.Value,
                    Title=model.Title,
                    Content=model.Content,
                    State=model.State.ValueShort(),
                    Language = model.Language.ValueShort(),
                    Highlight=model.Highlight,
                };

                int rs = NM.Edit(_new);
                if (rs != 1)
                    ViewData["msg"] = "操作失败，请稍后再试！".AlertOffPage();
                else
                    return Content(Url.Action("index").Redirect("保存成功！"));
                
            }

            return View(model);
        }

        public ActionResult Add()
        {
            var model = new EditNewModel
            {
                Lanaguages = (Enum.GetValues(typeof(Language)) as Language[]),
                Action="add",
                State=NewState.Enabled,
                Language=Language.En
            };
            return View("Edit",model);
        }

        [ValidateInput(false), HttpPost]
        public ActionResult Add(EditNewModel model)
        {

            if (ModelState.IsValid)
            {
                var _new = new New
                {
                    Title=model.Title,
                    Content=model.Content,
                    State=model.State.ValueShort(),
                    Language = model.Language.ValueShort(),
                    Highlight=model.Highlight,
                };

                int rs = NM.Add(_new);
                if (rs != 1)
                    ViewData["msg"] = "操作失败，请稍后再试！".AlertOffPage();
                else
                    return Content(Url.Action("add").Redirect("新闻发布成功"));
            }

            return View("Edit");
        }

        [ActionName("Index")]
        [MultiButton("Del")]
        public ActionResult Del(long[] NewID,IndexModel model)
        {
            string msg=null;
            if (NewID == null || NewID.Length == 0)
            {
                msg = "请先选中要删除的项！";
            }
            else
            {
                int rs = NM.Del(NewID);
                if (rs > 0)
                    msg = "成功删除" + rs + "条";
                else
                    msg = "操作失败，请稍后再试！";

            }
            ViewData["msg"] = msg.AlertOffPage();
            return Index(model);
        }
    }
}
